<template>
  <div>
    <el-card>
      <el-form :inline="true" :model="warehouseListReqForm">
        <el-row style="flex-wrap: wrap;" :gutter="10">
          <el-form-item label="仓库名称">
            <el-input v-model="warehouseListReqForm.warehouseName" placeholder="请输入仓库名称"></el-input>
          </el-form-item>
          <el-button class="searchbtn" @click="getWarehouseList" type="primary" icon="el-icon-search">搜索</el-button>

        </el-row>
      </el-form>
      <el-row :gutter="10">
        <el-col :span="24" style="text-align: right;">
          <el-button type="primary" size="mini" @click="addWarehouseBtnClick">添加仓库</el-button>
        </el-col>
      </el-row>
      <el-table :data="warehouseList" border>
        <el-table-column label="No" type="index"></el-table-column>
        <el-table-column label="仓库名称" prop="warehouseName" min-width="200"></el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="warehouseEditBtnClick(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="warehouseDeleteBtnClick(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="warehouseListReqForm.pageNum"
        :page-sizes="[10, 20, 30, 40]" :page-size="warehouseListReqForm.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <el-dialog v-dialogDrag title="添加仓库" :visible.sync="addWarehouseDialog" width="40%" center @closed="addWarehouseDialogClosed"
        :close-on-click-modal="false">
        <span style="text-align: center">
          <el-form ref="addWarehouseFormRef" :model="addWarehouseForm" :rules="addWarehouseFormRules" label-position="right"
            label-width="80px">
            <!--权限名称-->
            <el-form-item label="仓库名称" prop="warehouseName" v-on:keyup.enter.native="addWarehouse">
              <el-input v-model="addWarehouseForm.warehouseName"></el-input>
            </el-form-item>
          </el-form>
          <div>
            <el-button type="primary" @click="addWarehouse">提交</el-button>
          </div>
        </span>
      </el-dialog>
      <el-dialog v-dialogDrag title="编辑仓库" :visible.sync="editWarehouseDialog" width="40%" center @closed="editWarehouseDialogClosed"
        :close-on-click-modal="false">
        <span style="text-align: center">
          <el-form ref="editWarehouseFormRef" :model="editWarehouseForm" :rules="editWarehouseFormRules" label-position="right"
            label-width="80px">
            <!--权限名称-->
            <el-form-item label="仓库名称" prop="warehouseName" v-on:keyup.enter.native="editWarehouse">
              <el-input v-model="editWarehouseForm.warehouseName"></el-input>
            </el-form-item>
          </el-form>
          <div>
            <el-button type="primary" @click="editWarehouse">提交</el-button>
          </div>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Warehouse',
  data () {
    return {
      warehouseList: [],
      total: 0,
      addWarehouseDialog: false,
      editWarehouseDialog: false,
      warehouseListReqForm: {
        pageNum: 1,
        pageSize: 10,
        warehouseName: ''

      },
      addWarehouseForm: {
        warehouseName: ''
      },
      editWarehouseForm: {
        warehouseId: '',
        warehouseName: ''
      },
      addWarehouseFormRules: {
        warehouseName: [{
          required: true,
          message: '请输入仓库名称',
          trigger: 'blur'
        }]
      },
      editWarehouseFormRules: {
        warehouseName: [{
          required: true,
          message: '请输入仓库名称',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    async getWarehouseList () {
      const res = await this.$apiUtil.postByJson('warehouse/getWarehouseList', this.warehouseListReqForm)
      console.log(res)
      if (res.code !== '200') {
        return this.$message.error(res.msg)
      }
      this.warehouseList = res.data.warehouseVOS
      this.total = res.data.totalSize
    },
    handleSizeChange (newSize) {
      this.warehouseListReqForm.pageSize = newSize
      this.getWarehouseList()
    },
    handleCurrentChange (newPage) {
      this.warehouseListReqForm.pageNum = newPage
      this.getWarehouseList()
    },
    addWarehouseBtnClick () {
      this.addWarehouseDialog = true
    },
    async addWarehouse () {
      const res = await this.$apiUtil.postByJson('warehouse/addWarehouse', this.addWarehouseForm)
      console.log(res)
      if (res.code !== '200') {
        return this.$message.error(res.msg)
      }
      this.$message.success('添加仓库成功！')
      this.addWarehouseDialog = false
      this.getWarehouseList()
    },
    addWarehouseDialogClosed () {
      this.$refs.addWarehouseFormRef.resetFields()
    },
    editWarehouseDialogClosed () {
      this.$refs.editWarehouseFormRef.resetFields()
    },
    warehouseEditBtnClick (row) {
      this.editWarehouseDialog = true
      this.editWarehouseForm.warehouseId = row.warehouseId
      this.editWarehouseForm.warehouseName = row.warehouseName
    },
    async warehouseDeleteBtnClick (row) {
      this.$alert('确定删除仓库:' + row.warehouseName + ' ! ! !', '提示', {
        confirmButtonText: '确定',
        center: true,
        callback: async action => {
          if (action === 'confirm') {
            const res = await this.$apiUtil.postByForm('warehouse/deleteWarehouse', { warehouseId: row.warehouseId })
            console.log(res)
            if (res.code !== '200') {
              return this.$message.error(res.msg)
            }
            this.$message.success('删除仓库成功')
            this.getWarehouseList()
          }
        }
      })
    },
    async editWarehouse () {
      console.log(this.editWarehouseForm)
      const res = await this.$apiUtil.postByJson('warehouse/editWarehouse', this.editWarehouseForm)
      console.log(res)
      if (res.code !== '200') {
        return this.$message.error(res.msg)
      }
      this.$message.success('编辑仓库成功！')
      this.editWarehouseDialog = false
      this.getWarehouseList()
    }
  },
  created () {
    this.getWarehouseList()
  }
}
</script>

<style lang="less" scoped>
</style>
